package com.shop.cloud.controller.banner;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mapper.JsonMapper;
import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Banner;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.service.hongfu.IBannerService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

@RestController
@RequestMapping(value = "banner")
public class BannerController extends BaseController {

	private static final Logger logger=Logger.getLogger(BannerController.class);
	
	@Autowired
	private IBannerService bannerService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,@RequestBody Banner banner){
		banner.setStatus(0);
		banner.setOrderBy("BANNER.create_time DESC");
		Page<Banner> datas=bannerService.page(banner);
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(@CurrentUserId Long userId,@RequestBody Banner banner){
		banner.setStatus(0);
		if(banner.getSkipType()==0){
			banner.setSkipMsg("");
		}
		//已下架的banner重新上架
		if (banner.getSoldoutTime().getTime()>System.currentTimeMillis()) {
			banner.setIsDisplay(0);
		}
		bannerService.update(banner);
		logger.info("banner管理修改:"+ JsonMapper.nonDefaultMapper().toJson(banner));
		return ResponseUtil.ok();
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,@RequestBody Banner banner){
		banner.setStatus(0);
		banner.setCreateTime(new Timestamp((new Date()).getTime()));
		bannerService.insert(banner);
		logger.info("banner管理添加:"+ JsonMapper.nonDefaultMapper().toJson(banner));
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,@RequestBody Banner banner){
		banner.setStatus(1);
		bannerService.update(banner);
		return ResponseUtil.ok();
	}
	

}